<template>
  <v-chart
    ref="myChart"
    class="chart"
    :style="{ height }"
    :options="options"
  ></v-chart>
</template>

<script>
export default {
  props: {
    defaultIndex: {
      type: Number,
      default: undefined,
    },
    height: {
      type: String,
      default: "100%",
    },
    data: {
      type: Object,
      default: () => {},
    },
    name: {
      type: String,
      default: "",
    },
  },
  data() {
    return {};
  },
  computed: {
    options() {
      let echarts = this.$echarts;
      let { data } = this;
      //提案质量占比
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985",
            },
          },
        },
        legend: {
          show: false,
        },
        toolbox: {},
        grid: {
          left: "5%",
          right: "5%",
          top: "10%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: true,
            data: data.name,
            axisLabel: {
              show: true,
              textStyle: {
                color: "#333",
                fontSize: 10,
              },
              rotate: 30,
              interval: 0,
            },
          },
        ],
        yAxis: {
          type: "value",
          min: 0,
          max: 100,
          axisLabel: {
            formatter: "{value}分",
          },
        },
        dataZoom: [
          {
            show: true,
            realtime: true,
            start: 0,
            end: 50,
            height: 20,
            bottom: 5,
            textStyle: { color: "#333" },
          },
        ],
        series: [
          {
            name: "平均分",
            type: "bar",
            smooth: false,
            symbol: "none", //去掉折线点
            barWidth: 10,
            itemStyle: {
              normal: {
                //颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上
                color: "#3398DB", //背景渐变色
                lineStyle: {
                  // 系列级个性化折线样式
                  width: 2,
                  type: "solid",
                  color: "#3398DB",
                },
              },
            }, //线条样式
            areaStyle: {
              normal: {},
            },
            emphasis: {
              itemStyle: {
                shadowColor: "rgba(255, 255, 255, 0.8)",
                shadowBlur: 15,
              },
            },
            data: data.value,
          },
        ],
      };
      return option;
    },
  },
  mounted() {
    window.addEventListener("resize", this.resizeTheChart);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeTheChart);
  },
  methods: {
    resizeTheChart() {
      if (this.$refs.myChart) {
        this.$refs.myChart.resize();
      }
    },
    handleClick(e) {
      console.log("点击事件e:", e);
      if (e.dataIndex != this.defaultIndex) {
        this.$refs.myChart.dispatchAction({
          type: "downplay",
          seriesIndex: e.seriesIndex,
          dataIndex: this.defaultIndex,
        });
      }
      this.$refs.myChart.dispatchAction({
        type: "highlight",
        seriesIndex: e.seriesIndex,
        dataIndex: e.dataIndex,
      });
      if (e.dataIndex != this.currentIndex) {
        this.$refs.myChart.dispatchAction({
          type: "downplay",
          seriesIndex: 0,
          dataIndex: this.currentIndex,
        });
      }
      this.currentIndex = e.dataIndex;
      this.$emit("e-click", e, this.name);
    },
  },
  watch: {},
};
</script>

<style lang="stylus" scoped>
.chart {
  width: 100%;
}
</style>
